<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class MaterialContrast extends Model
{
    use HasFactory;

    protected $fillable = ['title','material_id','project_id','user_id','consult_price','consult_date','quotation_price','quotation_date','supplier','supplier_personal','pay_type'];

    const POSTONLY = ['title','material_id','project_id','user_id','consult_price','consult_date','quotation_price','quotation_date','supplier','supplier_personal','pay_type'];


    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public function project()
    {
        return $this->hasOne(Project::class,'id','project_id')->select('id','title','status');
    }

    public function material()
    {
        return $this->hasOne(Material::class,'id','material_id')->select('id','title');
    }

    public function user()
    {
        return $this->hasOne(User::class,'id','user_id')->select('id','name');
    }

    public function application()
    {
        return $this->hasOne(Application::class,'source_id','material_id')->where('model','MaterialContrast')->orderBy('id','desc');
    }
}
